import collections
class Solution(object):
    def findNumOfValidWords(self, words, puzzles):
        d = collections.defaultdict(int)
        for word in words:
            w = set(word)
            if len(w) <= 7:
                ans = 0
                for c in w:
                    ord_c = ord(c) - ord('a')
                    ans |= (1 << ord_c)
                d[ans] += 1
        out = [0] * len(puzzles)
        for index, word in enumerate(puzzles):
            w = ord(word[0]) - ord('a')
            for val in range(2 ** 6):
                ans = 1 << w
                for i in range(6):
                    if (val >> i) & 1:
                        ans |= (1 << (ord(word[i + 1]) - ord('a')))
                out[index] += d[ans]
        return out

data = Solution()
words = ["aaaa","asas","able","ability","actt","actor","access"]
puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
print(data.findNumOfValidWords(words, puzzles))
                

                                
                        


                        

            